package com.yanftch.hdc_tools.debugger;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.yanftch.hdc_tools.action.BaseAction;
import com.yanftch.hdc_tools.common.MessageDelegate;
import com.yanftch.hdc_tools.storage.PluginState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.yanftch.hdc_tools.command.CommandKt.hdc_list_targets;


public class TestAction extends BaseAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent action, @Nullable Project project) {
        PluginState service = project.getService(PluginState.class);
        String hdcPath = service.getHdcPath();
        String flCmd = com.yanftch.hdc_tools.common.ExtKt.finalCommandLine(hdcPath, hdc_list_targets);
        MessageDelegate.INSTANCE.info("flCmd=" + flCmd, action);
        String executeCommand = com.yanftch.hdc_tools.common.ExtKt.executeCommand(flCmd);
        MessageDelegate.INSTANCE.info("executeCommand=" + executeCommand, action);
//        String a = finalCommandLine(hdcPath, "list targets");
//        MessageDelegate.INSTANCE.info("a = " + a, action);
//        String s = executeCommand(a);
//        MessageDelegate.INSTANCE.info("s = " + s, action);


//        // 获取所有的环境变量
//        Map<String, String> getenv = System.getenv();
//
//        // 输出所有环境变量及其值
//        Set<String> keys = getenv.keySet();
//        for (String key : keys) {
//            System.out.println(key + "=" + getenv.get(key));
//        }

        // 启动APP
//        String bundleName = getBundleName(project);
//        String s = executeCommand("/Users/yanfeng/Documents/develop/harmonyOS/command-line-tools/sdk/HarmonyOS-NEXT-DB1/openharmony/toolchains/hdc uninstall com.yanftch.myapplication");
////        String s = executeProcessBuilder("hdc shell aa start -b com.yanftch.hdc_tools");
//        MessageDelegate.INSTANCE.info("sssss..." + s, action);


//        // 获取 bundleName
//        try {
//            String basePath = project.getBasePath();
//            Path configFilePath = Paths.get(basePath, "AppScope/app.json5");
//            if (Files.exists(configFilePath)) {
//                String configContent = new String(Files.readAllBytes(configFilePath));
//                JSONObject jsonObject = JSONObject.parseObject(configContent);
//                JSONObject obj2 = (JSONObject) jsonObject.get("app");
//                String bundleName = (String) obj2.get("bundleName");
//                String versionName = (String) obj2.get("versionName");
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
